package com.taobao.phoenix.qtdemo.dao;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
import com.taobao.phoenix.qtdemo.domain.Worker;

public class WorkerDao extends SqlMapClientDaoSupport {
	
	private static Logger LOG = LoggerFactory.getLogger("dao");
	
	public List<Worker> getWorkers(int machineId, int status) {
		try {
			Map<String, Object> parmas = new TreeMap<String, Object>();
			parmas.put("machine_id", machineId);
			parmas.put("status", status);
			List<Worker> result = getSqlMapClientTemplate().queryForList("worker.getWorkers", parmas);
			return result;
		} catch (DataAccessException e) {
			LOG.error("WorkerDao.getWorkers error, machineId="+machineId+", status="+status+", reason=", e);
			return Collections.emptyList();
		}
	}
	
	public boolean updateTalkNum(String nick, int talkNumDelta) {
		try {
			Map<String, Object> parmas = new TreeMap<String, Object>();
			parmas.put("nick", nick);
			parmas.put("talkNumDelta", talkNumDelta);
			parmas.put("time", new Date());
			int rows = getSqlMapClientTemplate().update("worker.updateTalkNum", parmas);
			return rows>0;
		} catch (DataAccessException e) {
			LOG.error("WorkerDao.updateTalkNum error, nick="+nick+", talkNumDelta="+talkNumDelta+", reason=", e);
			return false;
		}
	}
	
}
